5.1 - Adding DebugConsole commands


How to add our DebugConsole commands to the game?

Below we have our newly created mod using RedModManager.


using SonsSdk;

namespace DebugConsoleCommand;

public class DebugConsoleCommand : SonsMod
{
    public DebugConsoleCommand()
    {

        //HarmonyPatchAll = true;
    }

    protected override void OnInitializeMod()
    {
        Config.Init();
    }

    protected override void OnSdkInitialized()
    {
        DebugConsoleCommandUi.Create();
    }

    protected override void OnGameStart()
    {
    }
}
            

To add a DebugConsole command to the in game console (the one you open with F1) we only need to write one method with a special attribute above it.


[DebugCommand("commandname")]
private void MyCustomCommand()
{
    // what we want to do when entering our command will be typed here
}
            

  • commandname is the name we will need to type in the game console and that will be displayed in it.
  • MyCustomCommand is the name of the method and can be whatever you want.
If we go in a game and open the DebugConsole with F1, our custom made command should be here. Upon typing the command name and pressing enter, all the code code in between the curly braces will be executed.

How to pass parameters to the method

If we want the command to be toggleable like most of the default ones which accept on or off as a parameter, we need to pass a string parameter. To do so we write string followed by it's name in the method parameter like so:


[DebugCommand("commandname")]
private void MyCustomCommand(string onoff)
{
    // here we can use the onoff parameter to change how the command behave
}
            

The off string parameter is all the text we write after the command name, so if we write commandname hello in the game DebugConsole, hello will be our onoff parameter.

To make it more clear let's restore the aighostplayer command which was removed from the game:


// to use the VailActorManager.SetGhostPlayer() method we need to add Sons.Ai.Vail as dll reference

[DebugCommand("aighostplayer")]
private void AiGhostPlayer(string onoff)
{
    if (bool.TryParse(onoff, out var value))
    {
        VailActorManager.SetGhostPlayer(value); // will set invisibility from enemies
        SonsTools.ShowMessage($"Ran command aighostplayer {value}"); // used to print the bottom left message on screen
        return;
    }
    SonsTools.ShowMessage("Invalid syntax, usage: aighostplayer "); // used to print the bottom left message on screen
}
            

Here we used a bool.TryParse to convert the onoff string parameter to a boolean representation. When we use bool.TryParse, we need to pass the string as the first parameter and output the result as the second parameter if it successfully retrieved a boolean from the string we passed. The string that we can pass is either true or false. If the syntax is right, the VailActorManager.SetGhostPlayer(value); line will be executed, else nothing will happen.

Try copying the above code and do aighostplayer true in the game DebugConsole and you will see all enemies will ignore you. To turn it off do aighostplayer false.